package com.jinzhi.jzweb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jinzhi.common.domain.DictDO;
import com.jinzhi.common.utils.Result;
import com.jinzhi.common.utils.StringUtil;
import com.jinzhi.jzweb.domain.StuSignUpComDO;
import com.jinzhi.jzweb.domain.TrackDrawRegularDO;
import com.jinzhi.jzweb.service.StuSignUpComService;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jinzhi.jzweb.dao.TrackDrawDao;
import com.jinzhi.jzweb.domain.TrackDrawDO;
import com.jinzhi.jzweb.service.TrackDrawService;
import com.jinzhi.common.base.CoreServiceImpl;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 *
 * <pre>
 * 赛道抽签表（存储抽签信息）
 * </pre>
 * <small> 2023-10-16 16:36:18 | lance</small>
 */
@Service
public class TrackDrawServiceImpl extends CoreServiceImpl<TrackDrawDao, TrackDrawDO> implements TrackDrawService {

    @Autowired
    private StuSignUpComService stuSignUpComService;


    /**
     * 赛道抽签
     *
     * @param trackDrawDTO
     *          抽签逻辑：   新疆的婴幼儿保育赛项是团队赛，例如是45个队，90个人；分为1号赛道和2号赛道。1-45是1号赛道，46-90是2号赛道。
     *          每个队2名选手，随机分配到两个赛道，也就是一个在1-45号中不重复的随机号码，另外一个在46-90号中的不重复的随机号码。
     *
     * @return
     *
     */
    @Override
    public Result<List<TrackDrawDO>> selectDraw(TrackDrawDO trackDrawDTO) {
        if(trackDrawDTO.getDrawType()==1){
            return this.drawB();
        }else if(trackDrawDTO.getDrawType()==2){
            return this.drawC();
        }else{
            return Result.fail("抽签异常！未明确需要抽签的赛道！");
        }
    }


    /**
     * C赛道抽签
     * @return
     */
    public Result<List<TrackDrawDO>> drawC(){
        //查询所有参赛选手信息
        Wrapper<StuSignUpComDO> entityWrapper =new EntityWrapper<>();
        List<StuSignUpComDO> stuSignUpComDOS = stuSignUpComService.selectList(entityWrapper);
        if(null == stuSignUpComDOS && stuSignUpComDOS.size()>0){
            return Result.fail("没有参赛选手信息，无法抽签！");
        }
        //查询出B赛场的抽签结果
        List<TrackDrawDO> trackDrawDOS = this.selectList(new EntityWrapper<TrackDrawDO>().orderBy("sort",true));
//        if (null==trackDrawDOS || trackDrawDOS.isEmpty()) {
//            return Result.fail("请先对B赛场抽签！");
//        }
        // 初始化序号
        List<String> sessions = new LinkedList<>();
        for (int i = 1; i <= stuSignUpComDOS.size(); i++) {
            sessions.add(String.valueOf(i));
        }
        List<String> sessionsBack = new LinkedList<>(sessions);
        // 随机打乱院校顺序
        Random random = new Random();
        Collections.shuffle(sessions, random); // 随机打乱赛道
        //存储抽签记录
        List<TrackDrawDO> trackDrawDOList = new LinkedList<>();

        Map<String,TrackDrawDO> map = new HashMap<>();//存储已经抽签过的记录
        //固定赛道的选手会有先处理
        List<StuSignUpComDO> students = stuSignUpComDOS.stream()
                .sorted(Comparator.comparing(student -> student.getTrackDraw2(), Comparator.nullsLast(Comparator.naturalOrder())))
                .collect(Collectors.toList());
        //在循环院校中的选手 循环选手的时候注意 同有个院校的选手要在不同赛道（当选手多于赛道的时候，则允许同有个院校的选手同赛道）
        for (int i = 0; i < students.size(); i++) {
            TrackDrawDO trackDrawDO = new TrackDrawDO();
            trackDrawDO.setUnitName(students.get(i).getUnitName());//学校名称
            //先判断map中是否已经包含，包含则说明该学校已经有选手抽签过一次了，只需要抽第二位选手信息即可
            if(map.containsKey(students.get(i).getUnitName())){
                trackDrawDO = map.get(students.get(i).getUnitName());
                trackDrawDO.setName2(students.get(i).getName());
                //如果选手已经固定了，则不需要在随机分配啦
                if(StringUtils.isNotBlank(students.get(i).getTrackDraw2())){
                    trackDrawDO.setTrack2(students.get(i).getTrackDraw2());
                    sessions.remove(students.get(i).getTrackDraw2());//选手固定的序号
                }else {
                    //抽签
                    //先判断同校的选手是在1赛道还是2赛道，确保赛道不同
                    int num = 0;
                    if(Integer.valueOf(trackDrawDO.getTrack())<=sessionsBack.size()/2){
                        //随机数需要满足在2赛道
                        num = 0;
                        while (true){
                            //大于45 并且 sessions中含有该序号
                            if(Integer.valueOf(sessions.get(num))>sessionsBack.size()/2 && sessions.contains(sessions.get(num))){
                                break;
                            }
                            num++;
                        }
                    }else{
                        //随机数需要满足在1赛道
                        num = 0;
                        while (true){
                            if(Integer.valueOf(sessions.get(num))<=sessionsBack.size()/2 && sessions.contains(sessions.get(num))){
                                break;
                            }
                            num++;
                        }
                    }
                    trackDrawDO.setTrack2(String.valueOf(sessions.get(num)));
                    sessions.remove(String.valueOf(sessions.get(num)));
                }
            }else{
                //先抽一号选手的序号
                trackDrawDO.setName(students.get(i).getName());
                //如果选手已经固定了，则不需要在随机分配啦
                if(StringUtils.isNotBlank(students.get(i).getTrackDraw2())){
                    trackDrawDO.setTrack(students.get(i).getTrackDraw2());
                    sessions.remove(students.get(i).getTrackDraw2());//选手固定的序号
                }else {
                    trackDrawDO.setTrack(String.valueOf(sessions.get(0)));
                    sessions.remove(String.valueOf(sessions.get(0)));
                }
            }

            map.put(trackDrawDO.getUnitName(),trackDrawDO);
        }

        //如果已经抽了B,那么将C在B的基础上修改插入
        if(null!=trackDrawDOS &&  !trackDrawDOS.isEmpty()){
            for (TrackDrawDO trackDrawDO : trackDrawDOS) {
                //遍历map
                for (Map.Entry<String, TrackDrawDO> entry : map.entrySet()) {
                    if (entry.getKey().equals(trackDrawDO.getUnitName())) {
                        trackDrawDO.setTrack(entry.getValue().getTrack());
                        trackDrawDO.setTrack2(entry.getValue().getTrack2());
                        // 如果不再需要该键，可以移除它
                        map.remove(entry.getKey());
                        break; //一旦找到匹配项，就跳出循环
                    }
                }
            }
            this.updateBatchById(trackDrawDOS);
            return Result.ok(trackDrawDOS);
        }
        //如果没有抽B，先抽的C,那么需要将之前的抽签记录删除，然后再插入
        else{
            for (Map.Entry<String, TrackDrawDO> entry : map.entrySet()) {
                trackDrawDOList.add(entry.getValue());
            }
            //同时删除之前的抽签结果和批量保存抽签的结果
            EntityWrapper<TrackDrawDO> wrapper = new EntityWrapper<>();
            this.delete(wrapper);
            if(null != trackDrawDOList && trackDrawDOList.size()>0){
                this.insertBatch(trackDrawDOList);
            }
            return Result.ok(trackDrawDOList);
        }


    }


    /**
     * B赛道抽签
     * @param
     * @return
     */
    public Result<List<TrackDrawDO>> drawB(){
        //查询所有参赛选手信息
        Wrapper<StuSignUpComDO> entityWrapper =new EntityWrapper<>();
        List<StuSignUpComDO> stuSignUpComDOS = stuSignUpComService.selectList(entityWrapper);
        if(null == stuSignUpComDOS && stuSignUpComDOS.size()>0){
            return Result.fail("没有参赛选手信息，无法抽签！");
        }
        //查询出B赛场的抽签结果
        List<TrackDrawDO> trackDrawDOS = this.selectList(new EntityWrapper<TrackDrawDO>().orderBy("sort",true));
        // 初始化序号
        List<String> sessions = new LinkedList<>();
        for (int i = 1; i <= stuSignUpComDOS.size(); i++) {
            sessions.add(String.valueOf(i));
        }
        List<String> sessionsBack = new LinkedList<>(sessions);
        // 随机打乱院校顺序
        Random random = new Random();
        Collections.shuffle(sessions, random); // 随机打乱赛道
        //存储抽签记录
        List<TrackDrawDO> trackDrawDOList = new LinkedList<>();

        Map<String,TrackDrawDO> map = new HashMap<>();//存储已经抽签过的记录
        //固定赛道的选手会有先处理
        List<StuSignUpComDO> students = stuSignUpComDOS.stream()
                .sorted(Comparator.comparing(student -> student.getTrackDraw(), Comparator.nullsLast(Comparator.naturalOrder())))
                .collect(Collectors.toList());
        //在循环院校中的选手 循环选手的时候注意 同有个院校的选手要在不同赛道（当选手多于赛道的时候，则允许同有个院校的选手同赛道）
        for (int i = 0; i < students.size(); i++) {
            TrackDrawDO trackDrawDO = new TrackDrawDO();
            trackDrawDO.setUnitName(students.get(i).getUnitName());//学校名称
            //先判断map中是否已经包含，包含则说明该学校已经有选手抽签过一次了，只需要抽第二位选手信息即可
            if(map.containsKey(students.get(i).getUnitName())){
                trackDrawDO = map.get(students.get(i).getUnitName());
                trackDrawDO.setName2(students.get(i).getName());
                //如果选手已经固定了，则不需要在随机分配啦
                if(StringUtils.isNotBlank(students.get(i).getTrackDraw())){
                    trackDrawDO.setPlaceNum2(students.get(i).getTrackDraw());
                    sessions.remove(students.get(i).getTrackDraw());//选手固定的序号
                }else {
                    //抽签
                    //先判断同校的选手是在1赛道还是2赛道，确保赛道不同
                    int num = 0;
                    if(Integer.valueOf(trackDrawDO.getPlaceNum())<=sessionsBack.size()/2){
                        //随机数需要满足在2赛道
                        num = 0;
                        while (true){
                            //大于45 并且 sessions中含有该序号
                            if(Integer.valueOf(sessions.get(num))>sessionsBack.size()/2 && sessions.contains(sessions.get(num))){
                                break;
                            }
                            num++;
                        }
                    }else{
                        //随机数需要满足在1赛道
                        num = 0;
                        while (true){
                            if(Integer.valueOf(sessions.get(num))<=sessionsBack.size()/2 && sessions.contains(sessions.get(num))){
                                break;
                            }
                            num++;
                        }
                    }
                    trackDrawDO.setPlaceNum2(String.valueOf(sessions.get(num)));
                    sessions.remove(String.valueOf(sessions.get(num)));
                }
            }else{
                //先抽一号选手的序号
                trackDrawDO.setName(students.get(i).getName());
                //如果选手已经固定了，则不需要在随机分配啦
                if(StringUtils.isNotBlank(students.get(i).getTrackDraw())){
                    trackDrawDO.setPlaceNum(students.get(i).getTrackDraw());
                    sessions.remove(students.get(i).getTrackDraw());//选手固定的序号
                }else {
                    trackDrawDO.setPlaceNum(String.valueOf(sessions.get(0)));
                    sessions.remove(String.valueOf(sessions.get(0)));
                }
            }

            map.put(trackDrawDO.getUnitName(),trackDrawDO);
        }

        //遍历map
        for( Map.Entry<String,TrackDrawDO> entry : map.entrySet()){
            trackDrawDOList.add(entry.getValue());
        }
        //打乱”trackDrawDOList“顺序
        Collections.shuffle(trackDrawDOList, random);
        int i = 1;//添加序号
        for (TrackDrawDO trackDrawDO : trackDrawDOList) {
            trackDrawDO.setSort(i++);
        }



        //如果已经抽了C,那么将B在C的基础上修改插入
        if(null!=trackDrawDOS &&  !trackDrawDOS.isEmpty()){
            for (TrackDrawDO trackDrawDO : trackDrawDOS) {
                //遍历map
                for (Map.Entry<String, TrackDrawDO> entry : map.entrySet()) {
                    if (entry.getKey().equals(trackDrawDO.getUnitName())) {
                        trackDrawDO.setPlaceNum(entry.getValue().getPlaceNum());
                        trackDrawDO.setPlaceNum2(entry.getValue().getPlaceNum2());
                        // 如果不再需要该键，可以移除它
                        map.remove(entry.getKey());
                        break; //一旦找到匹配项，就跳出循环
                    }
                }
            }
            this.updateBatchById(trackDrawDOS);
        }
        //如果没有抽C，先抽的B,那么需要将之前的抽签记录删除，然后再插入
        else{
            //同时删除之前的抽签结果和批量保存抽签的结果
            EntityWrapper<TrackDrawDO> wrapper = new EntityWrapper<>();
            this.delete(wrapper);
            if(null != trackDrawDOList && trackDrawDOList.size()>0){
                this.insertBatch(trackDrawDOList);
            }
        }


        return Result.ok(trackDrawDOList);
    }


    /**
     * 查询上一次的抽签结果
     * @param trackDrawDTO
     * @return
     */
    @Override
    public TrackDrawDO trackList(TrackDrawDO trackDrawDTO) {
//存储抽签你信息
        TrackDrawDO resultVO = new TrackDrawDO();
        resultVO.setHasDraw(true);//默认为true.不存在上次抽签结果时改为false

        //查询上一次的抽签结果
        Wrapper<TrackDrawDO> wrapper = new EntityWrapper<>();
        wrapper.orderBy(true,"sort");
        List<TrackDrawDO> trackDrawDOS = this.selectList(wrapper);

        //如果没有抽签上一次抽签结果那就默认初始化
        if(trackDrawDOS==null || trackDrawDOS.size()==0){
            resultVO.setHasDraw(false);
            Wrapper<StuSignUpComDO> entityWrapper =new EntityWrapper<>();
            List<StuSignUpComDO> stuSignUpComDOS = stuSignUpComService.selectList(entityWrapper);
            if(stuSignUpComDOS!=null && stuSignUpComDOS.size()>0){
                // 对同一院校的选手分组
                Map<String, List<StuSignUpComDO>> trackDrawByUnitMap = stuSignUpComDOS.stream()
                        .collect(Collectors.groupingBy(StuSignUpComDO::getUnitName));
                for (String s : trackDrawByUnitMap.keySet()) {
                    TrackDrawDO trackDrawDO = new TrackDrawDO();
                    trackDrawDO.setUnitName(s);
                    trackDrawDOS.add(trackDrawDO);
                    for (int i = 0; i < trackDrawByUnitMap.get(s).size(); i++) {
                        String name = trackDrawByUnitMap.get(s).get(i).getName();
                        if (i==0){
                            trackDrawDO.setName(name);
//                            trackDrawDO.setPlaceNum("1");
                        } else if (i==1) {
                            trackDrawDO.setName2(name);
//                            trackDrawDO.setPlaceNum2("99");
                        }else {
                            break;
                        }
                    }
                }
            }
        }
        resultVO.setTrackDrawDOList(trackDrawDOS);

        return resultVO;
    }


    /**
     * 赛道抽签
     *
     * @param trackDrawDTO
     *          抽签逻辑：   新疆的婴幼儿保育赛项是团队赛，目前报名情况是45个队，90个人；分为1号赛道和2号赛道。1-45是1号赛道，46-90是2号赛道。
     *          每个队2名选手，随机分配到两个赛道，也就是一个在1-45号中不重复的随机号码，另外一个在46-90号中的不重复的随机号码。
     *
     * @return
     *
     */
//    @Override
//    public Result<List<TrackDrawDO>> selectDraw(TrackDrawDO trackDrawDTO) {
//        //查询所有参赛选手信息
//        Wrapper<StuSignUpComDO> entityWrapper =new EntityWrapper<>();
//        List<StuSignUpComDO> stuSignUpComDOS = stuSignUpComService.selectList(entityWrapper);
//        if(null == stuSignUpComDOS && stuSignUpComDOS.size()>0){
//            return Result.fail("没有参赛选手信息，无法抽签！");
//        }
//        // 初始化序号
//        List<String> sessions = new LinkedList<>();
//        for (int i = 1; i <= stuSignUpComDOS.size(); i++) {
//            sessions.add(String.valueOf(i));
//        }
//        List<String> sessionsBack = new LinkedList<>(sessions);
//        // 随机打乱院校顺序
//        Random random = new Random();
//        Collections.shuffle(sessions, random); // 随机打乱赛道
//        //存储抽签记录
//        List<TrackDrawDO> trackDrawDOList = new LinkedList<>();
//
//        int j = 0;//同于记录排序
//        Map<String,TrackDrawDO> map = new HashMap<>();//存储已经抽签过的记录
//        List<StuSignUpComDO> students = stuSignUpComDOS.stream()
//                .sorted(Comparator.comparing(student -> student.getTrackDraw(), Comparator.nullsLast(Comparator.naturalOrder())))
//                .collect(Collectors.toList());
//        //在循环院校中的选手 循环选手的时候注意 同有个院校的选手要在不同赛道（当选手多于赛道的时候，则允许同有个院校的选手同赛道）
//        for (int i = 0; i < students.size(); i++) {
//            TrackDrawDO trackDrawDO = new TrackDrawDO();
//            trackDrawDO.setUnitName(students.get(i).getUnitName());//学校名称
//            //先判断map中是否已经包含，包含则说明该学校已经有选手抽签过一次了，只需要抽第二位选手信息即可
//            if(map.containsKey(students.get(i).getUnitName())){
//                trackDrawDO = map.get(students.get(i).getUnitName());
//                trackDrawDO.setName2(students.get(i).getName());
//                //如果选手已经固定了，则不需要在随机分配啦
//                if(StringUtils.isNotBlank(students.get(i).getTrackDraw())){
//                    trackDrawDO.setPlaceNum2(students.get(i).getTrackDraw());
//                    sessions.remove(students.get(i).getTrackDraw());//选手固定的序号
//                }else {
//                    //抽签
//                    //先判断同校的选手是在1赛道还是2赛道，确保赛道不同
//                    int num = 0;
//                    if(Integer.valueOf(trackDrawDO.getPlaceNum())<=sessionsBack.size()/2){
//                        //随机数需要满足在2赛道
//                        num = 0;
//                        while (true){
//                            //大于45 并且 sessions中含有该序号
//                            if(Integer.valueOf(sessions.get(num))>sessionsBack.size()/2 && sessions.contains(sessions.get(num))){
//                                break;
//                            }
//                            num++;
//                        }
//                    }else{
//                        //随机数需要满足在1赛道
//                        num = 0;
//                        while (true){
//                            if(Integer.valueOf(sessions.get(num))<=sessionsBack.size()/2 && sessions.contains(sessions.get(num))){
//                                break;
//                            }
//                            num++;
//                        }
//                    }
//                    trackDrawDO.setPlaceNum2(String.valueOf(sessions.get(num)));
//                    sessions.remove(String.valueOf(sessions.get(num)));
//                }
//            }else{
//                //先抽一号选手的序号
//                trackDrawDO.setName(students.get(i).getName());
//                //如果选手已经固定了，则不需要在随机分配啦
//                if(StringUtils.isNotBlank(students.get(i).getTrackDraw())){
//                    trackDrawDO.setPlaceNum(students.get(i).getTrackDraw());
//                    sessions.remove(students.get(i).getTrackDraw());//选手固定的序号
//                }else {
//                    trackDrawDO.setPlaceNum(String.valueOf(sessions.get(0)));
//                    sessions.remove(String.valueOf(sessions.get(0)));
//                }
//            }
//
//            map.put(trackDrawDO.getUnitName(),trackDrawDO);
//        }
//
//        //遍历map
//        for( Map.Entry<String,TrackDrawDO> entry : map.entrySet()){
//            trackDrawDOList.add(entry.getValue());
//        }
//        //打乱”trackDrawDOList“顺序
//        Collections.shuffle(trackDrawDOList, random);
//        int i = 1;//添加序号
//        for (TrackDrawDO trackDrawDO : trackDrawDOList) {
//            trackDrawDO.setSort(i++);
//        }
//
//        //同时删除之前的抽签结果和批量保存抽签的结果
//        EntityWrapper<TrackDrawDO> wrapper = new EntityWrapper<>();
//        this.delete(wrapper);
//        if(null != trackDrawDOList && trackDrawDOList.size()>0){
//            this.insertBatch(trackDrawDOList);
//        }
//
//
//        return Result.ok(trackDrawDOList);
//    }


    /**
     * 广东赛道抽签  规则：
     *  如总共一个40个学校，80个选手，分为A、B两个组，第一次先抽A ,第二次抽B,一个学校的选手一个在A,一个在B
     * @param trackDrawDTO
     * @return
     */
    @Override
    public Result<List<TrackDrawDO>> selectDrawGuangDong(TrackDrawDO trackDrawDTO) {
        if(trackDrawDTO.getDrawType()==1){
            return this.drawGuangDongA();
        }else if(trackDrawDTO.getDrawType()==2){
            return this.drawGuangDongB();
        }else{
            return Result.fail("抽签异常！未明确需要抽签的赛道！");
        }

    }

    private List<StuSignUpComDO> stuSignUpComDOList = new ArrayList<>();



    public Result<List<TrackDrawDO>> drawGuangDongA(){
        //查询所有参赛选手信息
        Wrapper<StuSignUpComDO> entityWrapper =new EntityWrapper<>();
        List<StuSignUpComDO> stuSignUpComDOS = stuSignUpComService.selectList(entityWrapper);
        if(null == stuSignUpComDOS && stuSignUpComDOS.size()>0){
            return Result.fail("没有参赛选手信息，无法抽签！");
        }
        stuSignUpComDOList = new ArrayList<>();
        //存储最终的抽签结果
        List<TrackDrawDO> trackDrawDOList = new ArrayList<>();

        // 打乱"stuSignUpComDOS"顺序
        Collections.shuffle(stuSignUpComDOS);
        // 对同一院校的选手分组
        Map<String, List<StuSignUpComDO>> trackDrawByUnitMap = stuSignUpComDOS.stream()
                .collect(Collectors.groupingBy(StuSignUpComDO::getUnitName));

        /**
         * 打乱“trackDrawByUnitMap”的顺序，用“shuffledMap”保存打乱后的数据 等于是在打乱A\B赛场的顺序
         */
        // 将键转换为列表
        List<String> keys = new ArrayList<>(trackDrawByUnitMap.keySet());
        // 对键进行排序
        Collections.shuffle(keys); // 打乱键的顺序
        // 创建一个新的有序 LinkedHashMap，按照排序后的顺序将键和值放入其中
        Map<String, List<StuSignUpComDO>> shuffledMap = new LinkedHashMap<>();
        for (String key : keys) {
            shuffledMap.put(key, trackDrawByUnitMap.get(key));
        }
        int i = 1;
        for (Map.Entry<String, List<StuSignUpComDO>> entry : shuffledMap.entrySet()){
            //打乱两位选手的顺序
            Collections.shuffle(entry.getValue());
            //取每个的第一条当作第一天的抽签结果
            TrackDrawDO trackDrawDO = new TrackDrawDO();
            trackDrawDO.setUnitName(entry.getKey());
            trackDrawDO.setName(entry.getValue().get(0).getName());
            trackDrawDO.setPlaceNum(i + "-" + entry.getValue().get(0).getName());
            trackDrawDO.setTrack(i + "");
            trackDrawDOList.add(trackDrawDO);
            //存未抽签的数据
            stuSignUpComDOList.add(entry.getValue().get(1));
            i++;
        }

        //打乱trackDrawDOList顺序
        Collections.shuffle(trackDrawDOList);
        int j = 1;
        for (TrackDrawDO trackDrawDO : trackDrawDOList) {
            trackDrawDO.setSort(j++);
        }

        //同时删除之前的抽签结果和批量保存抽签的结果
        EntityWrapper<TrackDrawDO> wrapper = new EntityWrapper<>();
        this.delete(wrapper);
        if(!trackDrawDOList.isEmpty()){
            this.insertBatch(trackDrawDOList);
        }


        return Result.ok(trackDrawDOList);
    }

    public Result<List<TrackDrawDO>> drawGuangDongB(){
        //查询出A的抽签结果
        List<TrackDrawDO> trackDrawDOS = this.selectList(new EntityWrapper<TrackDrawDO>().orderBy("sort",true));
        //打乱未抽签的顺序 然后依次分配序号
        Collections.shuffle(stuSignUpComDOList);
        for (int i = 1; i <= stuSignUpComDOList.size(); i++) {
            //暂且用state当作排序
            stuSignUpComDOList.get(i-1).setState(i);
        }

        if(null!=trackDrawDOS &&  !trackDrawDOS.isEmpty()){
            for (TrackDrawDO trackDrawDO : trackDrawDOS) {
                //遍历
                for (StuSignUpComDO stuSignUpComDO : stuSignUpComDOList) {
                    if (stuSignUpComDO.getUnitName().equals(trackDrawDO.getUnitName())) {
                        trackDrawDO.setName2(stuSignUpComDO.getName());
                        trackDrawDO.setPlaceNum2(stuSignUpComDO.getState() + "-" + stuSignUpComDO.getName());
                        trackDrawDO.setTrack2(stuSignUpComDO.getState() + "");
                        break; //一旦找到匹配项，就跳出循环
                    }
                }
            }
            this.updateBatchById(trackDrawDOS);
            return Result.ok(trackDrawDOS);
        }


        return Result.ok(trackDrawDOS);
    }


}
